package divide;

import divide.ColumnDivider.DividingResult;

/**
 * Class for printing results of program work.
 *
 * @author Muzalevskiy Vitaliy
 */
public class Printer {

    /**
     * This method prints the result of division in form of column.
     *
     * @param dividingResult - value that include all results of division.
     */
    public static void printColumn(DividingResult dividingResult){
        printFirstLine(dividingResult.getDividend(), dividingResult.getDivisor());
        printSecondLine(dividingResult);
        int[] intermidiateValueArray = dividingResult
                .getDividentIntermidiateArray();
        int[] dividentIntermidiateArray = dividingResult
                .getDividentIntermidiateArray();
        printSirdLine(dividingResult);
        printElseColumn(dividingResult);
        printRest(dividingResult.getDividend(), dividingResult.getRest());
    }
    
    private static void printFirstLine(int dividend, int divisor){
        System.out.println(dividend + "|" + divisor); 
    }
    
    private static void printSecondLine(DividingResult dividingResult){
        countPrinter(String.valueOf(dividingResult.getDividend()).length(), " ");
        System.out.print("|");
        countPrinter(getLongerValueLength(dividingResult.getDivisor(),
                dividingResult.getResult()), "-");
        System.out.println();
    }
    
    private static void printSirdLine(DividingResult dividingResult){
        System.out.print(dividingResult.getIntermidiateValueArray()[0]);
        countPrinter((String.valueOf(dividingResult.getDividend()).length() - 
                String.valueOf(dividingResult.getDividentIntermidiateArray()[0])
                .length()), " ");
        System.out.print("|");
        System.out.println(dividingResult.getResult());
        countPrinter(String.valueOf(dividingResult.getDividentIntermidiateArray()[0])
                .length(), "-");
        System.out.println();
    }
    
    private static void printElseColumn (DividingResult dividingResult){
        int count = 1;
        while(dividingResult.getDividentIntermidiateArray()[count] != 0){            
            prindDivideIntermidiateValue(dividingResult, count);
            printIntermidiateValue(dividingResult, count);
            printeColumnSeparator(dividingResult, count);
            count++;
        }
    }
    
    private static void prindDivideIntermidiateValue(DividingResult dividingResult, int count) {
        System.out.println(additionalLength(String.valueOf(
                dividingResult.getDividentIntermidiateArray()[0])
                .length() + count - 1, String.valueOf(dividingResult
                .getDividentIntermidiateArray()[count]).length())
                + dividingResult.getDividentIntermidiateArray()[count]);
    }
    
    private static void printIntermidiateValue(DividingResult dividingResult, int count) {
        System.out.println(additionalLength(String.valueOf(
                dividingResult.getIntermidiateValueArray()[0])
                .length() + count - 1, String.valueOf(dividingResult
                .getIntermidiateValueArray()[count]).length())
                + dividingResult.getIntermidiateValueArray()[count]);
    }
    
    private static void printeColumnSeparator(DividingResult dividingResult, int count) {
        System.out.print(additionalLength(String.valueOf(
                dividingResult.getDividentIntermidiateArray()[0])
                .length() + count - 1, String.valueOf(dividingResult
                .getDividentIntermidiateArray()[count]).length()));
        countPrinter(getLongerValueLength(
                dividingResult.getDividentIntermidiateArray()[count], 
                dividingResult.getIntermidiateValueArray()[count]), "-");
        System.out.println();
    }
    
    private static void printRest(int divident, int rest){
        System.out.println(additionalLength(String.valueOf(divident).length() - 1, 
                String.valueOf(rest).length()) + rest);
    }
    
    /**
     * This method return to console value of second parameter 
     * <code> value </code> in quantity that noted in first parameter.
     * 
     * @param count quantity of values need to be printed.
     * @param value value need to be printed.
     */
    private static void countPrinter(int count, String value){
        for(int i = 0; i < count; i++){
            System.out.print(value); 
        }
    }
    
    /**
     * This method fill with backspace quantity of symbols that equal to
     * difference between parameters of the method.
     * 
     * @param first - length of first value.
     * @param second - length of second value.
     * @return 
     */
    private static String additionalLength(int first, int second){
        StringBuilder whiteSpace = new StringBuilder();
        for(int i = 0; i <= first - second; i++){
            whiteSpace.append(" ");
        }
        return whiteSpace.toString();
    }
    
    /**
     * Method return one of two values that is longer than another.
     * 
     * @param first - length of first value.
     * @param second - length of second value.
     * @return 
     */
    private static int getLongerValueLength(int first, int second){
        int length;
        if(String.valueOf(first).length() < String.valueOf(second).length()){
            length = String.valueOf(second).length();
        }
        else{
            length = String.valueOf(first).length();
        }
        return length;
    }
    
    /**
     * Print to console result and rest values of division.
     *
     * @param dividingResult - value that include all results of division.
     */
    public static void printResult(DividingResult dividingResult){
        System.out.println();
        System.out.println("Dividing results:");
        System.out.println("result = " + dividingResult.getResult());
        System.out.println("rest = " + dividingResult.getRest());
    }
}
